//
// Created by 田建昆 on 2020/12/21.
//

#ifndef CODEBASE_LIST_H
#define CODEBASE_LIST_H

#include <stdlib.h>
#include <string.h>

typedef struct list_node list_node_t;
typedef struct list list_t;

struct list_node {
    void *data;
    list_node_t *next;
};

struct list {
    list_node_t *head;
    list_node_t *tail;
    void (*destroy)(void *data);
    int (*match)(const void *key1, const void *key2);
    int size;
};

#define list_head(list) ((list)->head)
#define list_tail(list) ((list)->tail)
#define list_len(list) ((list)->size)
#define list_is_head(list, node) ((list)->head == (node) ? 1 : 0)
#define list_is_tail(node) ((node)->next == NULL ? 1 : 0)
#define list_data(node) ((node)->data)
#define list_next(node) ((node)->next)

void list_init(list_t *list, void (*destroy)(void *data));
void list_destroy(list_t *list);
int list_ins_next(list_t *list, list_node_t *node,  void *data);
int list_rem_next(list_t *list, list_node_t *node, void **data);

#endif //CODEBASE_LIST_H
